package com.vtquang.mymaps;
import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XMLParseWeatherInformation extends DefaultHandler {
	
	WeatherInformation weather;
	ArrayList<WeatherInformation> lstWeatherInfor;
	StringBuilder sb = null;
	
	boolean bdate = false;
	boolean btempMax = false;
	boolean btempMin = false;
	boolean burlIcon = false;
	boolean bItem = false;
    public XMLParseWeatherInformation(){
    	weather = new WeatherInformation();
    	lstWeatherInfor = new ArrayList<WeatherInformation>();
    }
    public ArrayList<WeatherInformation> getListWeatherInformation(){
    	return this.lstWeatherInfor;
    }
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		super.characters(ch, start, length);
		if (bdate || btempMax || btempMin || burlIcon) {
			String str = new String(ch, start, length);
			sb.append(str);
		} 
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		super.endElement(uri, localName, qName);
		
		if (localName.equals("weather")) { 
		    lstWeatherInfor.add(weather);
			bItem = false; 
		} else if (localName.equals("date")) {
			weather.setDate(sb.toString());
			bdate = false;
		} else if (localName.equals("tempMaxC")) {
			weather.setTempMaxC(Integer.parseInt(sb.toString()));
			btempMax = false;
		} else if (localName.equals("tempMinC")) { 
			weather.setTempMinC(Integer.parseInt(sb.toString()));
			btempMin = false;
		} else if (localName.equals("weatherIconUrl")) {
			weather.setWeatherIconUrl(sb.toString());
			burlIcon = false;
		}
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		super.startElement(uri, localName, qName, attributes);
		
		if (localName.equals("weather")) {
			weather = new WeatherInformation();
			sb = new StringBuilder("");
			bItem = true;
		} else if (localName.equals("date")) {
			sb = new StringBuilder("");
			bdate = true;
		} else if (localName.equals("tempMaxC")) {
			sb = new StringBuilder("");
			btempMax = true;
		} else if (localName.equals("tempMinC")) {
			sb = new StringBuilder("");
			btempMin = true;
		} else if (localName.equals("weatherIconUrl")) {
			sb = new StringBuilder("");
			burlIcon = true;
		}
		} 
}
